﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text;

namespace Kang.ExtendMethod
{
    public static class StreamExtendMethod
    {
        /// <summary>
        /// 保存流数据至文件
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="path"></param>
        /// <param name="overwrite"></param>
        /// <returns></returns>
        public static bool SaveToFile(this Stream stream, string path, bool overwrite = true)
        {
            if (stream == null)
            {
                return false;
            }
            if (!stream.CanRead)
            {
                return false;
            }
            if (File.Exists(path) && overwrite != true)
            {
                return false;
            }
            FileMode mode = overwrite ? FileMode.Create : FileMode.CreateNew;
            try
            {
                using (FileStream fs = new FileStream(path, mode, FileAccess.ReadWrite, FileShare.Read))
                {
                    int readCount = 0;
                    int lenCount = 1024;
                    byte[] data = new byte[lenCount];
                    while ((readCount = stream.Read(data, 0, lenCount)) > 0)
                    {
                        fs.Write(data, 0, readCount);
                        fs.Flush();
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 流数据追加至文件
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="path"></param>
        /// <param name="overwrite"></param>
        /// <returns></returns>
        public static bool AppendToFile(this Stream stream, string path)
        {
            if (stream == null)
            {
                return false;
            }
            if (!stream.CanRead)
            {
                return false;
            }
            if (!File.Exists(path))
            {
                return false;
            }
            try
            {
                using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Read))
                {
                    int readCount = 0;
                    int lenCount = 1024;
                    byte[] data = new byte[lenCount];
                    while ((readCount = stream.Read(data, 0, lenCount)) > 0)
                    {
                        fs.Write(data, 0, readCount);
                        fs.Flush();
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Stream转Byte数组
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static byte[] GetBytes(this Stream stream)
        {
            if (stream == null)
            {
                return new byte[0];
            }
            if (!stream.CanRead)
            {
                return new byte[0];
            }
            byte[] _data = null;
            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    int readCount = 0;
                    int lenCount = 1024;
                    byte[] data = new byte[lenCount];
                    while ((readCount = stream.Read(data, 0, lenCount)) > 0)
                    {
                        ms.Write(data, 0, readCount);
                        ms.Flush();
                    }
                    _data = ms.ToArray();
                    ms.Dispose();
                }
                return _data;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Stream转字符串，默认UTF-8
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string AsString(this Stream stream, Encoding encoding = null)
        {
            if (stream == null)
            {
                return string.Empty;
            }
            if (!stream.CanRead)
            {
                return string.Empty;
            }
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }
            try
            {
                using (StreamReader reader = new StreamReader(stream, encoding))
                {
                    return reader.ReadToEnd();
                }
            }
            catch
            {
                return string.Empty;
            }
        }
    }
}
